package org.realogic.cfm.persistence.element;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class KLongElement implements KElement<Long> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5237548797939896729L;

	private static final Long ZERO = Long.valueOf(0);
	private Long value;

	public KLongElement() {

	}

	public KLongElement(Long i) {
		this.value = i;
	}

	public KLongElement(long i) {
		this.value = Long.valueOf(i);
	}

	public Long getValue() {
		return value;
	}

	public void setValue(Long value) {
		this.value = value;
	}

	public Long getDefaultValue() {
		return ZERO;
	}

	public int getSqlType() {
		return Types.BIGINT;
	}

	public String getName() {
		return "long";
	}

	public Class<Long> getClass_() {
		return Long.class;
	}

	public String format(Object value) {
		return format((Long) value);
	}

	public Long get(ResultSet rs, String name) throws SQLException {
		return Long.valueOf((rs.getLong(name)));
	}

	public Long get(ResultSet rs, int index) throws SQLException {
		return Long.valueOf((rs.getLong(index)));
	}

	public KElement<Long> getElement(ResultSet rs, int index) throws SQLException {
		return new KLongElement(rs.getLong(index));
	}

	public void set(PreparedStatement st, int index, Long value) throws SQLException {
		if (value == null) {
			st.setNull(index, getSqlType());
		} else {
			st.setLong(index, value.longValue());
		}
	}

	public void set(PreparedStatement st, int index, long value) throws SQLException {
		st.setLong(index, value);
	}

	public String toString() {
		return value == null ? "" : value.toString();
	}
}
